
anomalyTable = ["RAM saturation", "WARNING ! disk almost full", "Disk full"]
tabLength = anomalyTable.length

Thread.new{
  while 1
    sleep 210
    tabComputer = []
    randValue = (0...tabLength).sort_by{rand}[1]
    
    case randValue
        when 0 then
            ram_use = 2000
            ram_total = 2000
            ram_percent = 100
            disk_use = 40000
            disk_total = 120000 
            disk_percent = 34
        when 1 then
            ram_use = 1000
            ram_total = 2000
            ram_percent = 50
            disk_use = 105000
            disk_total = 120000 
            disk_percent = 90
        when 2 then
            ram_use = 1000
            ram_total = 2000
            ram_percent = 50
            disk_use = 120000
            disk_total = 120000 
            disk_percent = 100
    end
    
    @machines = Machine.find_by_sql "SELECT id FROM machines ORDER BY RAND()"
    
    @machines.each do |machine|
        tabComputer.push("#{machine.id}")
    end
    
    puts "\n\n*******************************\n\n"
    puts "Creation d'anomalie. Tu l'as senti ?"
    @incident = Incident.create(:machine_id => tabComputer.first,
            :incident_type => "#{anomalyTable[randValue]}",
            :ram_use => ram_use,
            :ram_total => ram_total,
            :ram_percent => ram_percent,
            :disk_use => disk_use,
            :disk_total => disk_total,
            :disk_percent => disk_percent,
            :created_at => Time.now,
            :updated_at => Time.now)
      
    if @incident.save
       puts "oh oui!! c'est l'incident #{@incident.id} sur la machine #{tabComputer.first}"
    else
       puts "non game over. Try again!" 
    end
    
    puts "\nIncident genere de type '#{anomalyTable[randValue]}' \n\n"
    puts "*******************************\n\n"
  end
}
